import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';

var _info;
var _context;

class DetailPage extends StatelessWidget {

  final info;

  DetailPage(this.info) {
    _info = info;
  }

  @override
  Widget build(BuildContext context) {
    _context = context;

    return MaterialApp(
      home: ShowDetail(),
    );
  }

}

class ShowDetail extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return _ShowDetail();
  }

}

class _ShowDetail extends State {

  List<String> _favorite = [];

  SharedPreferences prefs;

  @override
  Widget build(BuildContext context) {
    // print(_info);

    var material = _info["material"];
    List<Widget> materialList = [];

    for (var i = 0; i < material.length; i++) {
       var child = Row(
         children: [
           Expanded(child: Text(material[i]["mname"], textAlign: TextAlign.center,)),
           Expanded(child: Text(material[i]["amount"], textAlign: TextAlign.center,))
         ],
      );

      materialList.add(child);
    }

    var process = _info["process"];
    List<Widget> processList = [];

    for (var i = 0; i < process.length; i++) {
      var index = i + 1;
      processList.add(
          Html(
            data: "$index、" + process[i]["pcontent"],
          )
      );
      processList.add(
        Image(
          image: NetworkImage(process[i]["pic"]),
          width: MediaQuery.of(context).size.width,
          height: 150,
        )
      );
    }

    return Scaffold(
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.favorite),
        onPressed: () async {
          //print("favorite");
          if (_favorite.contains(jsonEncode(_info))) {
            //print("已经收藏了");

            Fluttertoast.showToast(
              msg: "你已经收藏过该菜谱啦！",
              toastLength: Toast.LENGTH_SHORT,
              gravity: ToastGravity.BOTTOM,
            );

            return;
          }
          saveFavorite();
        },
      ),
      appBar: AppBar(
        brightness: Brightness.dark,
        title: Text(_info["name"]),
        centerTitle: true,
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () {
            Navigator.of(_context).pop();
          },
        ),
      ),
      body: SingleChildScrollView(
        physics: BouncingScrollPhysics(),
        child: Container(
          padding: EdgeInsets.all(8),
          child: Center(
            child: Column(
              children: [
                Image(
                  fit: BoxFit.cover,
                  image: NetworkImage(_info["pic"]),
                  width: MediaQuery.of(context).size.width,
                  height: 200,
                ),
                Html(data: _info["content"]),
                Text(
                  "材料",
                  textAlign: TextAlign.center,
                  style: TextStyle(fontWeight: FontWeight.bold),
                ),
                Container(
                  child: Column(
                    children: materialList,
                  ),
                ),
                Text(
                  "做法",
                  textAlign: TextAlign.center,
                  style: TextStyle(fontWeight: FontWeight.bold),
                ),
                Container(
                  child: Column(
                    children: processList,
                  ),
                )
              ],
            ),
          ),
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();

    favoriteData();
  }

  void favoriteData() async {
    prefs = await SharedPreferences.getInstance();
    List<String> _list = prefs.getStringList("favorite");
    if (_list != null) {
      _favorite.clear();
      _favorite.addAll(_list);
    }
  }

  void saveFavorite() {
    _favorite.add(jsonEncode(_info));
    //print(_favorite);
    String msg = "收藏失败！";

    if (prefs != null) {
      prefs.setStringList("favorite", _favorite);
      msg = "收藏成功！";
    }

    Fluttertoast.showToast(
      msg: msg,
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.BOTTOM,
    );
  }

}